window.onload=function(){
    var config = {
        elem:"#activityList",
        url:'',
        cols:[[
            {field:'id', width:80, title: '编号',sort:true},
            {field:'active_name', width:400, title: '活动名称'},
            {field:'active_num', width:150, title: '成团人数',sort:true},
            {field:'begin_time', width:160, title: '有效期',templet: function (item) {
                    var start_time = timestampToTime(parseInt(item.begin_time));
                    var end_time =  timestampToTime(parseInt(item.end_time));
                    return start_time + '  --  '+ end_time;
                }}
            ,{field:'status', width:150, title: '状态',templet: function (item) {
                    switch (parseInt(item.status)){
                        case 0:
                            return '暂停中';
                        case 1:
                            return '进行中';
                        case 2:
                            return '过期';
                        case 3:
                            return '禁用';
                    }
                }},
            {field:'right', minWidth:150, title: '操作',align:'center',toolbar:'#operation3'}
        ]]
    };
    layui.use(['element','table','jquery','layer','form'],function(){
        var table=layui.table,
            form = layui.form,
            $ = layui.jquery;
        // 活动列表
        var id = $('#id').val();
        var url = '/shop/activity?id=' + id;
        config.url = url;
        table.render(config);

        table.on('tool(activityList)',function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'detail'){
                location.href = '/shop/select?id='+data.id;
            }else if(layEvent === 'prohibit'){
                Utils.layer.confirm('是否禁用该活动？',{
                    btn:['禁用','再想想']
                },function(){
                    Utils.layer.closeAll();
                    Utils.layer.load(1,{'shade':0.5});
                    $.ajax({
                        url: '/shop/end',
                        data:{id:data.id},
                        method:'post',
                        dataType:'json',
                        success:function (data) {
                            Utils.layer.closeAll();
                            Utils.layer.msg(data.msg);
                            table.render(config);
                        }
                    });
                })
            }
        });
        //搜索
        form.on("submit(serch-form)",function(data){
            var query = $("#form1").serialize();
            var url = '/shop/activity?' + query;
            config.url = url;
            table.render(config);
            return false;

        });

        var hf=window.location.href.split("?");
        var hf2=hf[1].split("&");
        var hf3=hf2[1];
        var id=hf2[0].split("=")[1];
        if(hf3!=undefined){
            $(".a1").attr("href","/shop/get-info?id="+id+"&type=1");
            $(".a2").attr("href","/shop/goods-index?id="+id+"&type=1");
            $(".a3").attr("href","/shop/activity?id="+id+"&type=1");
        }else{
            $(".a1").attr("href","/shop/get-info?id="+id);
            $(".a2").attr("href","/shop/goods-index?id="+id);
            $(".a3").attr("href","/shop/activity?id="+id);
        }
        
        $('.back').on("click",function(){
            if(hf3!=undefined){
                this.href="/shop-user/index";
            }else{
                this.href="/shop/index";
            }
        })

    });
};